package cn.com.xinjinfu.pdoulcProduct.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.adapter.PagerAdapter;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.fragment.MyInviest.BiddingListFragment;
import cn.com.xinjinfu.pdoulcProduct.fragment.MyInviest.FinishListFragment;
import cn.com.xinjinfu.pdoulcProduct.fragment.MyInviest.RefunddingListFragment;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.utils.LoginRequestUtils;
import cn.com.xinjinfu.pdoulcProduct.view.LoadStatusBox;

/**
 * 我的投资列表
 * Created by android-adt on 2015/12/31.
 */
public class MyInvestListAct extends BaseActivity implements View.OnClickListener{
    private int xItem = 0;
    private ViewPager mViewPager;
    private PagerAdapter mPagerAdapter;
    private TextView tv_refundding,tv_bidding,tv_finish;
    private LoadStatusBox loadbox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_invest_list);
        ActivityManager.getInstance().addActivity(this);
        //初始化头布局
        TitleManager.showTitle(this, "我的投资", "");
        Bundle bundle = new Bundle();
        bundle = this.getIntent().getExtras();
        xItem = bundle.getInt("xItem");
        initView();
        loadbox = (LoadStatusBox) this.findViewById(R.id.loadStatusBox);
        loadbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                netCheck();
            }
        });
        loadbox.loading();
        netCheck();
    }

    private void netCheck(){
        if (!DataHandler.isNetworkConnected(this)) {
            loadbox.failed();
            ToastManager.show(this, "请检查网络连接");
            return;
        }else {
            loadbox.success();
        }
    }
    private void initView() {
        tv_refundding = (TextView) findViewById(R.id.tv_refundding);
        tv_bidding = (TextView) findViewById(R.id.tv_bidding);
        tv_finish = (TextView) findViewById(R.id.tv_finish);

        tv_refundding.setOnClickListener(this);
        tv_bidding.setOnClickListener(this);
        tv_finish.setOnClickListener(this);

        mViewPager = (ViewPager)findViewById(R.id.view_pager);
        mPagerAdapter = new PagerAdapter(this);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                L.d("position=============++++" + position);
                if (position == 0) {
                    tv_refundding.setBackgroundColor(0xffF4B942);
                    tv_bidding.setBackgroundColor(0xffCCCCCC);
                    tv_finish.setBackgroundColor(0xffCCCCCC);
                } else if (position == 1) {
                    tv_refundding.setBackgroundColor(0xffCCCCCC);
                    tv_bidding.setBackgroundColor(0xffF4B942);
                    tv_finish.setBackgroundColor(0xffCCCCCC);
                } else {
                    tv_refundding.setBackgroundColor(0xffCCCCCC);
                    tv_bidding.setBackgroundColor(0xffCCCCCC);
                    tv_finish.setBackgroundColor(0xffF4B942);
                }
            }
        });
        /***
         * 此处可以用一个类，创建3个对象添加进来
         */
        mPagerAdapter.addTab(RefunddingListFragment.class, null);
        mPagerAdapter.addTab(BiddingListFragment.class, null);
        mPagerAdapter.addTab(FinishListFragment.class, null);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(xItem, true);//购买成功后直接跳转到 投标中
    }

    @Override
    public void onClick(View v) {
        switch (v.getId() ) {
            case R.id.tv_refundding:
                tv_refundding.setBackgroundColor(0xffF4B942);
                tv_bidding.setBackgroundColor(0xffCCCCCC);
                tv_finish.setBackgroundColor(0xffCCCCCC);
                mViewPager.setCurrentItem(0, true);
                break;
            case R.id.tv_bidding:
                tv_refundding.setBackgroundColor(0xffCCCCCC);
                tv_bidding.setBackgroundColor(0xffF4B942);
                tv_finish.setBackgroundColor(0xffCCCCCC);
                mViewPager.setCurrentItem(1, true);
                break;
            case R.id.tv_finish:
                tv_refundding.setBackgroundColor(0xffCCCCCC);
                tv_bidding.setBackgroundColor(0xffCCCCCC);
                tv_finish.setBackgroundColor(0xffF4B942);
                mViewPager.setCurrentItem(2, true);
                break;
        }
    }
    private final String ACTION_NAME = "跳转首页广播";

    @Override
    public void onBackPressed() {
        if (xItem == 1) {
            Intent mIntent = new Intent(ACTION_NAME);
            mIntent.putExtra("yaner", "mPdou");
            sendBroadcast(mIntent);
            finish();
        } else{
            finish();
        }
    }
}
